
#include "include/task_dispatcher.h"

#include <chrono>
#include <functional>
#include <iostream>

#include "include/task_dispatcher_impl.h"
#include "include/thread_pool.h"

namespace dispatcher
{

bool Task::Wait() const
{
    if (unlikely(TaskType::Last == type_))
    {
        return false;
    }

    while (likely(State::Finished != state_))
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }

    return true;
}

TaskDispatcher::TaskDispatcher(uint32_t task_thread_n) : impl_sptr_(std::make_shared<TaskDispatcherImpl>())
{
    thread_pool::ThreadPool::GetInstance()->Initialize(task_thread_n);
}

void TaskDispatcher::AddTask(TaskSptr task, Task::TaskType type)
{
    impl_sptr_->AddTaskImpl(task, type);
}

}  // namespace dispatcher
